<?php
/**
 * @link https://www.leadshop.vip/
 * @copyright Copyright ©2020-2021 浙江禾成云计算有限公司
 */
namespace system\api;

use framework\common\BasicController;
use Yii;
use yii\web\ForbiddenHttpException;
use yii\web\ServerErrorHttpException;


/**
 * 后台用户管理器
 */
class MenusController extends BasicController
{
    public $modelClass = 'system\models\Menus';


    /**
     * 用户菜单
     * @param  string $value [description]
     * @return [type]        [description]
     */
    public function actionMenus()
    {
        //获取角色模型
        $rolesModel = 'system\models\Roles';
        //获取角色模型
        $menusModel = $this->modelClass;
        //获取用户对应角色
        $roles  = Yii::$app->user->identity->roles;

        if($roles!='all') {
            $roles  = json_decode(Yii::$app->user->identity->roles, true);
        }
        if($roles!='all') {
            $rolesArray = $rolesModel::find()->where(['is_deleted' => 0])->where(['in', 'id', $roles])
                ->orderBy('id asc')->all();
            $visits = [];
            foreach ($rolesArray as $key => $value) {
                $visitArray = json_decode($value->visits, true);
                //数组合并
                $visits = array_merge($visits, $visitArray);
            }
            //数组去重
            $visits = array_unique($visits);
        }
        //查询菜单
        if($roles!='all') {
            $menus= $menusModel::find()
                //设置菜单别名，缩短代码量
                //->where(['tag' => 'modules'])
                ->where(['is_deleted' => 0]);

                     $menus= $menus->andWhere(['in', 'id', $visits])->andWhere(['parent_id' => 0])
                ->asArray()
                ->all();
        }else{
            $menus= $menusModel::find()
                //设置菜单别名，缩短代码量
                //->where(['tag' => 'modules'])
                ->where(['is_deleted' => 0])->andWhere(['parent_id' => 0])
                ->asArray()
                ->all();
        }
        $modelClass = $this->modelClass;
        if($roles!='all') {
            foreach ($menus as $keyb => $value) {
                $list = $modelClass::find()->where(['parent_id' => $value['id']])->andWhere(['is_deleted' => 0]);
                $list = $list->andWhere(['in', 'id', $visits]);
                $list = $list->asArray()->orderBy('sort,id ASC')->all();
                if ($list) {
                    foreach ($list as $kbce => $veb) {
                        $list2 = $modelClass::find()->where(['parent_id' => $veb['id']])->andWhere(['is_deleted' => 0]);
                        $list2 = $list2->andWhere(['in', 'id', $visits]);
                        $list2 = $list2->asArray()->orderBy('sort,id asc')->all();
                        if ($list2) {
                            $list[$kbce]['list'] = $list2;
                        } else {
                            $list[$kbce]['list'] = '';
                        }
                    }
                    $menus[$keyb]['list'] = $list;
                } else {
                    $menus[$keyb]['list'] = '';
                }

            }
        }else{
            foreach ($menus as $keyb => $value) {
                $list = $modelClass::find()->where(['parent_id' => $value['id']])->andWhere(['is_deleted' => 0]);
                $list = $list->asArray()->orderBy('sort,id ASC')->all();
                if ($list) {
                    foreach ($list as $kbce => $veb) {
                        $list2 = $modelClass::find()->where(['parent_id' => $veb['id']])->andWhere(['is_deleted' => 0]);
                        $list2 = $list2->asArray()->orderBy('sort,id asc')->all();
                        if ($list2) {
                            $list[$kbce]['list'] = $list2;
                        } else {
                            $list[$kbce]['list'] = '';
                        }
                    }
                    $menus[$keyb]['list'] = $list;
                } else {
                    $menus[$keyb]['list'] = '';
                }

            }
        }
        return $menus;
    }


    /**
     * 用于返回所有菜单信息
     * @return [type] [description]
     */
    public function actionIndex()
    {
        $get        = Yii::$app->request->get();
        $modelClass = $this->modelClass;
        if (isset($get['type'])) {
            $model = $modelClass::find()->where(['is_deleted' => 0, 'name' => $get['type']])->one();
            return $modelClass::find()
                ->where(['like', 'path', $model['path'] . '-' . $model['id']])
                ->andWhere(['is_deleted' => 0])
                ->all();
        } else {
            return $modelClass::find()->where(['is_deleted' => 0])->all();
        }
    }

    /**
     * 处理数据搜索问题
     * @return [type] [description]
     */
    public function actionTree()
    {
        $modelClass = $this->modelClass;
        $model      = $modelClass::find()->select(['id', 'title', 'path'])->where(['parent_id' => 0])->andWhere(['is_deleted' => 0])->asArray()->orderBy('sort,id ASC')->all();
        foreach ($model as $key => $value) {
//            $number               = count(explode('-', $value['path'])) - 1;
//            $title                = str_repeat("|-", $number) . $value['title'];
//            $model[$key]['title'] = $title;
            $list               = $modelClass::find()->where(['parent_id' => $value['id']])->andWhere(['is_deleted' => 0])->asArray()->orderBy('sort,id ASC')->all();
            foreach ($list as $kbce=>$veb) {
                $list2 = $modelClass::find()->where(['parent_id' => $veb['id']])->andWhere(['is_deleted' => 0])
                   ->asArray()->orderBy('sort,id ASC')->all();
                if ($list2) {
                    $list[$kbce]['list'] = $list2;
                } else {
                    $list[$kbce]['list'] = '';
                }
            }
            $model[$key]['list'] = $list;

        }
        return $model;
    }

    /**
     * 处理数据分页问题
     * @return [type] [description]
     */
    public function actionDelete()
    {
        $get        = Yii::$app->request->get();
        $modelClass = $this->modelClass;
        $id         = intval($get['id']);
        $model      = $modelClass::findOne($id);
        if ($model) {
            $subset = $modelClass::find()->where(['parent_id' => $id])->andWhere(['is_deleted' => 0])->all();
            if ($subset) {
                throw new ServerErrorHttpException('检测有子集存在，无法删除，请先删除子集');
            } else {
//                $model->deleted_time = time();
//                $model->is_deleted   = 1;
                $data=['deleted_time'=>time(),'is_deleted'=>1];
                $result =  $modelClass::updateAll($data, ['id' => $id]);
                if ($result) {
                   // Yii::$app->getResponse()->setStatusCode(204);
                    return $result;
                } else {
                    throw new ForbiddenHttpException('删除失败，请检查is_deleted字段是否存在');
                }
            }

        } else {
            throw new ForbiddenHttpException('删除失败，数据不存在');
        }
    }

    /**
     * 处理数据分页问题
     * @return [type] [description]
     */
    public function actionGather()
    {
        $get        = Yii::$app->request->get();
        $modelClass = $this->modelClass;
        return $modelClass::find()->where(['is_deleted' => 0])->indexBy("id")->all();
    }

    /**
     * 处理数据搜索问题
     * @return [type] [description]
     */
    public function actionSearch()
    {
        $modelClass = $this->modelClass;
        $data       = \Yii::$app->request->post();
        return $modelClass::find()->where(['like', 'title', $data['keyword']])->all();
    }

}
